<%= render 'spree/admin/shared/product_tabs', :current => 'Product Properties' %>
<%= render 'spree/shared/error_messages', :target => @product %>

<% admin_breadcrumb(plural_resource_name(Spree::ProductProperty)) %>

<% content_for :page_actions do %>
  <% if can?(:create, Spree::ProductProperty) %>
    <ul class="tollbar inline-menu">
      <li>
        <%= link_to_add_fields Spree.t(:add_product_properties), 'tbody#product_properties', :class => 'plus button' %>
      </li>
    </ul>
  <% end %>
<% end %>

<%= form_for @product, :url => admin_product_url(@product), :method => :put do |f| %>
  <fieldset>
    <legend align="center"><%= plural_resource_name(Spree::ProductProperty) %></legend>
    <div class="add_product_properties" data-hook="add_product_properties"></div>

    <table class="index sortable" data-hook data-sortable-link="<%= update_positions_admin_product_product_properties_url %>">
      <thead>
        <tr data-hook="product_properties_header">
          <th colspan="2"><%= Spree::Property.model_name.human %></th>
          <th><%= Spree::ProductProperty.human_attribute_name(:value) %></th>
          <th class="actions"></th>
        </tr>
      </thead>
      <tbody id="product_properties" data-hook>
        <%= f.fields_for :product_properties do |pp_form| %>
          <%= render 'product_property_fields', :f => pp_form %>
        <% end %>
      </tbody>
    </table>

    <% if can?([:create, :update], Spree::ProductProperty) %>
      <%= render 'spree/admin/shared/edit_resource_links' %>
    <% end %>

    <%= hidden_field_tag 'clear_product_properties', 'true' %>
  </fieldset>
<% end %>

<%= form_tag admin_product_product_properties_path, method: :get, id: 'variant_option_value_selections' do %>
  <fieldset class='no-border-bottom'>
    <legend align="center"><%= Spree.t(:variant_properties) %></legend>
    <fieldset class='no-border-top'>
      <% @option_types.each do |option_type, option_values| %>
        <div class="field">
          <%= label :option_type_presentation, option_type.presentation %>
          <%= select_tag "ovi[]", options_from_collection_for_select(option_values, :id, :presentation, params[:ovi]), class: 'select2 fullwidth', include_blank: true, id: "#{option_type.name}_option_type_select" %>
        </div>
      <% end %>
      <div class="form-buttons filter-actions actions">
        <%= button Spree.t(:filter_results) %>
        <% if @option_value_ids.present? %>
          <%= link_to_add_fields Spree.t(:add_variant_properties), 'tbody#variant_property_values', class: 'plus button' %>
        <% end %>
      </div>
    </fieldset>
  </fieldset>
<% end %>

<%= form_for @product, url: admin_product_url(@product), method: :put do |f| %>
  <%= f.fields_for :variant_property_rules, @variant_property_rule do |rule_form| %>
    <%= rule_form.hidden_field 'id', value: @variant_property_rule.id %>
    <%= rule_form.hidden_field 'option_value_ids', value: @option_value_ids.join(',') %>
    <% if @option_value_ids.present? %>
      <fieldset class='no-border-top'>
        <table class="index sortable" data-hook data-sortable-link="<%= update_positions_admin_product_variant_property_rule_values_url %>">
          <thead>
            <tr data-hook="variant_property_values_header">
              <th colspan="2"><%= Spree::Property.model_name.human %></th>
              <th><%= Spree::ProductProperty.human_attribute_name(:value) %></th>
              <th class="actions"></th>
            </tr>
          </thead>
          <tbody id="variant_property_values" data-hook>
            <%= rule_form.fields_for :values do |values_form| %>
              <%= render 'product_property_fields', f: values_form %>
            <% end %>
          </tbody>
        </table>
        <% if can?([:create, :update], Spree::VariantPropertyRule) %>
          <%= render 'spree/admin/shared/edit_resource_links' %>
        <% end %>
      </fieldset>
    <% end %>
  <% end %>
<% end %>

<%= javascript_tag do -%>
  var properties = <%= raw(@properties.to_json) %>;
  $('#product_properties, #variant_property_values').on('keydown', 'input.autocomplete', function() {
    already_auto_completed = $(this).is('ac_input');
    if (!already_auto_completed) {
      $(this).autocomplete({source: properties});
      $(this).focus();
    }
  });
<% end -%>
